package com.we.connect.weconnect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.we.connect.weconnect.mapper.system.SysUserMapper;
import com.we.connect.weconnect.model.system.SysUser;
import com.we.connect.weconnect.model.system.vo.PasswordVo;
import com.we.connect.weconnect.service.SysUserService;
import com.we.connect.weconnect.utils.DateUtils;
import com.we.connect.weconnect.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Boolean registerSysUser(SysUser sysUser) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", sysUser.getUserName());
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper);

        if(sysUsers.size() != 0){
            return false;
        }

        sysUser.setCreateTime(DateUtils.getTime());
        int insert = sysUserMapper.insert(sysUser);
        if(insert != 1){
            return false;
        }
        return true;
    }

    @Override
    public SysUser loginSysUser(SysUser sysUser) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", sysUser.getUserName());
        wrapper.eq("password", sysUser.getPassword());
        SysUser user = sysUserMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public Boolean updatePassword(PasswordVo passwordVo, SysUser loginSysUser) {
        if(loginSysUser.getPassword().equals(passwordVo.getOldPassword())){
            loginSysUser.setPassword(passwordVo.getNewPassword());
            sysUserMapper.updateById(loginSysUser);
            return true;
        }
        return false;
    }
}
